package com.fun.feifun.base.auth;

import cn.dev33.satoken.stp.StpInterface;
import com.fun.feifun.modules.sys.auth.model.SysMenu;
import com.fun.feifun.modules.sys.auth.model.SysRole;
import com.fun.feifun.modules.sys.auth.service.ISysMenuService;
import com.fun.feifun.modules.sys.auth.service.ISysRoleService;
import com.fun.feifun.modules.sys.user.service.ISysUserService;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: wukefei
 * @Date: Created in 2022/4/22 15:58
 * @Description:
 * @Version: 1.0
 *
 */
@Component
public class StpInterfaceImpl implements StpInterface{

	@Resource
	private ISysMenuService menuService;
	@Resource
	private ISysUserService userService;
	@Resource
	private ISysRoleService roleService;


	@Override
	public List<String> getPermissionList(Object o, String s) {
		Long userId = Long.parseLong(o.toString());
		//找到角色列表
		List<SysRole> roleList =  new ArrayList<>();//userService.getRoleByUserId(userId);
		List<Long> roleIds = roleList.stream().map(it -> it.getId()).collect(Collectors.toList());
		//找到权限列表
		List<SysMenu> menuList = menuService.getListRoleIds(roleIds);
		List<String> permitList = CollectionUtils.isEmpty(menuList)?null: menuList.stream().map(it -> it.getPermit()).collect(Collectors.toList());
		return permitList;
	}

	@Override
	public List<String> getRoleList(Object o, String s) {
		Long userId = Long.parseLong(o.toString());
		//找到角色列表
		List<SysRole> roleList = new ArrayList<>();//userService.getRoleByUserId(userId);
		List<String> list =  CollectionUtils.isEmpty(roleList)?null:roleList.stream().map(it -> it.getRoleCode()).collect(Collectors.toList());
		return list;
	}



}
